﻿using System;
using System.Runtime.InteropServices;

namespace GLDotNet
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Viewport : IEquatable<Viewport>
    {
        public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Viewport));
        
        /// <summary>
        /// The X coordinate of the lower left corner of the viewport.
        /// </summary>
        public int X;

        /// <summary>
        /// The Y coordinate of the lower left corner of the viewport.
        /// </summary>
        public int Y;

        /// <summary>
        /// The width of the viewport.
        /// </summary>
        public int Width;

        /// <summary>
        /// The height of the viewport.
        /// </summary>
        public int Height;
                
        public Viewport(int x, int y, int width, int height)
        {
            this.X = x;
            this.Y = y;
            this.Width = width;
            this.Height = height;
        }

        /// <summary>
        /// Creates a Viewport from the given size with X and Y set to 0.
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static Viewport FromSize(Size size)
        {
            return new Viewport(0, 0, size.Width, size.Height);
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (!(obj is Viewport))
                return false;

            return this.Equals((Viewport)obj);
        }

        public bool Equals(Viewport other)
        {
            return this.X == other.X &&
                   this.Y == other.Y &&
                   this.Width == other.Width &&
                   this.Height == other.Height;
        }

        public static bool operator ==(Viewport v1, Viewport v2)
        {
            return v1.Equals(v2);
        }

        public static bool operator !=(Viewport v1, Viewport v2)
        {
            return !v1.Equals(v2);
        }
    }
}
